CODE 65. Plus One

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/07/2013-10-07-CODE 65 Plus One/

访问原文「CODE 65. Plus One

####
Given a number represented as an array of digits, plus one to the number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public int[] plusOne(int[] digits) {
// Start typing your Java solution below
// DO NOT write main() function
int c = 1;
int i;
for (i = digits.length - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i] += c;
break;
} else if (digits[i] == 9) {
digits[i] = 0;
}
}
if (i < 0 && digits[0] == 0) {
int[] newDigits = new int[digits.length + 1];
newDigits[0] = 1;
return newDigits;
} else {
return digits;
}
}
Jerky Lu wechat
欢迎加入微信公众号